﻿#include "itemwidget.h"
#include "ui_itemwidget.h"

ItemWidget::ItemWidget(QWidget *parent,
                       const QString &table_,
                       const QString &category_) :
    BaseEditor(parent),
    ui(new Ui::ItemWidget)
{
    ui->setupUi(this);

    selectId=0;
    table=table_;
    category=category_;

    model=new QSqlTableModel(this);
    ui->tableView->setModel(model);

    createMenu();
    updateView();   //手动更新
}

ItemWidget::~ItemWidget()
{
    delete ui;
}

void ItemWidget::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key()==Qt::Key_S)
        save();
    else if(event->modifiers() == Qt::ControlModifier && event->key()==Qt::Key_R)
        cancelItem();
    else if(event->modifiers() == Qt::ControlModifier && event->key()==Qt::Key_D)
        finishItem();
    else
    {
        switch (event->key()) {
        case Qt::Key_F1:
            newItem();
            break;
        case Qt::Key_F5:
            updateView();
            break;
        }
    }
}

void ItemWidget::generateMenu()
{
    menu->addAction(QStringLiteral("新记录... (F1)"),this,&ItemWidget::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&ItemWidget::updateView);
    menu->addSeparator();
    QMenu *m_trans=menu->addMenu("转到->");
    if(category != CAT_MEMO)
        m_trans->addAction("备忘",this,&ItemWidget::changeToMemo);

    if(category != CAT_SCHEDULE)
        m_trans->addAction("计划",this,&ItemWidget::changeToSchedule);

    m_trans->addAction("事务 (已完成)",this,&ItemWidget::finishItem);
    menu->addSeparator();
    menu->addAction("取消 (Ctrl+R)",this,&ItemWidget::cancelItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("保存 (Ctrl+S)"),this,&ItemWidget::save);
}

void ItemWidget::save()
{
    if(model->rowCount() < 1)
        return;

    if(selectId <= 0)
        return;

    QString detail=ui->detail->toPlainText();
    if(detail.isEmpty() || detail == previousDetail)
        return;

    if(sp_updateColumnById(table,"detail",detail,selectId))
        updateView();
    else
        MESSAGE_CANNOT_SAVE
}

void ItemWidget::changeCategory(const QString &category)
{
    if(model->rowCount() < 1)
        return;

    if(selectId <= 0)
        return;

    if(sp_updateColumnById(table,"category",category,selectId))
        updateView();
    else
        MESSAGE_CANNOT_UPDATE
}

void ItemWidget::changeStatus(const QString &status)
{
    if(model->rowCount() < 1)
        return;

    if(selectId <= 0)
        return;

    if(sp_updateColumnById(table,"addition",status,selectId))
        updateView();
    else
        MESSAGE_CANNOT_UPDATE
}

void ItemWidget::changeToSchedule()
{
    changeCategory(CAT_SCHEDULE);
}

void ItemWidget::changeToMemo()
{
    changeCategory(CAT_MEMO);
}

void ItemWidget::cancelItem()
{
    changeStatus(ST_CANCEL);
}

void ItemWidget::finishItem()
{
    changeCategory(CAT_JOURNAL);
}

void ItemWidget::newItem()
{
    IW_AddItem item(this,table,category);
    item.setWindowTitle(QStringLiteral("新") + category);
    if(item.exec() == QDialog::Accepted)
        updateView();
}

void ItemWidget::updateView()
{
    if(category.isEmpty())
        return;

    model->setTable(table);
    model->setFilter(QString("category='%1' AND addition != '%2'")
                     .arg(category)
                     .arg(ST_CANCEL));
    model->select();

    setHeaderData();
}

void ItemWidget::setHeaderData()
{
    ui->tableView->hideColumn(ID_COL);   //id
    ui->tableView->hideColumn(MONTH_COL);   //month_
    model->setHeaderData(DATE_COL,Qt::Horizontal,QStringLiteral("日期")); //date_
    ui->tableView->hideColumn(CAT_COL);     //category
    model->setHeaderData(DET_COL,Qt::Horizontal,category); //detail
    ui->tableView->hideColumn(ADD_COL);
    // ui->tableView->setColumnWidth(DET_COL,800);
    // model->setHeaderData(ADD_COL,Qt::Horizontal,QStringLiteral("备注")); //addition

    // if(model->rowCount() > 0)
    //    ui->tableView->resizeRowsToContents();

    ui->detail->clear();
}

void ItemWidget::on_tableView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectId=sp_fetchId(index);
        selectDate=sp_fetchString(index,DATE_COL);
        previousDetail=sp_fetchString(index,DET_COL);
        ui->detail->setPlainText(previousDetail);
    }
}

void ItemWidget::on_tableView_doubleClicked(const QModelIndex &)
{
    if(previousDetail.isEmpty())
        return;

    qApp->clipboard()->setText(selectDate + " " + previousDetail);
}

void ItemWidget::on_key_editingFinished()
{
    if(category.isEmpty())
        return;

    QString key=ui->key->text().trimmed();
    if(key.isEmpty())
        return;

    model->setTable(table);
    model->setFilter(QString("category='%1' AND detail like '%%2%'")
                     .arg(category)
                     .arg(key));
    model->select();

    setHeaderData();
}
